/**
 * mobile core
 * @author Levin
 * @version 1.0
 */
JF.M("mcore", (function($) {
	var p = {},
	pub = {};
	p.support={
		userAgentStr:window.navigator.userAgent.toLowerCase()
	};
	p.support.isAndroid=(p.support.userAgentStr.search('android') > -1);
	p.support.isiOS=(p.support.userAgentStr.search('iphone') > -1 ||
			p.support.userAgentStr.search('ipad') > -1 || 
			p.support.userAgentStr.search('ipod') > -1 || 
			p.support.userAgentStr.search('playbook') > -1);
	p.support.positionFixedSupport=(p.support.isAndroid && ('onhashchange' in window));
	p.support.cssTransitions= ("WebKitTransitionEvent" in window);

	p.touchable=function(){
		var touchSupport=true;
		if (! ('createTouch' in document)){
			try {
				var touchevent = document.createEvent('TouchEvent');
				if (!touchevent.initTouchEvent) {
					touchSupport = false;
				};
			} catch(ex) {
				touchSupport = false
			};//try
		};//if
		return touchSupport;
	};
	p.fastclick = {
		touchable:true,
		trackingClick:true,
		clickStart:{
			x:0,
			y:0,
			scroll:0
		},
		elmFromPoint:function(){
			var targetElement = document.elementFromPoint(this.clickStart.x, this.clickStart.y + ((p.support.isAndroid && !p.support.positionFixedSupport) ? window.pageYOffset: 0));
			return targetElement;
		},
		onTouchStart:function(e){
			var base=p.fastclick;
			base.trackingClick = true;
			base.clickStart.x = e.targetTouches[0].clientX;
			base.clickStart.y = e.targetTouches[0].clientY;
			base.clickStart.scroll = window.pageYOffset;
			return true;
		},
		onTouchMove:function(e){
			var base=p.fastclick;
			if (base.trackingClick) {
				if (
					Math.abs(e.targetTouches[0].clientX - base.clickStart.x) > 10 || 
					Math.abs(e.targetTouches[0].clientY - base.clickStart.y) > 10 ) {
					base.trackingClick = false;
				};
			};
			return true;
		},
		onTouchEnd:function(e){
			var base=p.fastclick;
			if (!base.trackingClick || 
				//_columnsAnimating || 
				Math.abs(window.pageYOffset - base.clickStart.scroll) > 5) { 
				return true;
			};
			var targetElement = base.elmFromPoint();
			if (targetElement.nodeType == Node.TEXT_NODE) {
				targetElement = targetElement.parentNode;
			};
			var clickEvent = document.createEvent('MouseEvents');
			clickEvent.initEvent('click', true, true);
			clickEvent.forwardedTouchEvent = true;
			targetElement.dispatchEvent(clickEvent);
			if (targetElement.className.indexOf('needsclick') != -1) return false;
			if (targetElement.nodeName != 'SELECT') e.preventDefault();
		},
		onTouchCancel:function(e){
			p.fastclick.trackingClick = false;
		},
		onClick:function(e){
			if (!window.event) return true;

			var allowClick = true,
				base=p.fastclick,
				targetElement = base.elmFromPoint();

			if (targetElement && targetElement.className.indexOf('needsclick') != -1) return true;

			if (!window.event.forwardedTouchEvent && base.touchable) {
				allowClick = false
			};
			if (allowClick) return true;
			e.stopPropagation();
			e.preventDefault();
			return false;
		},
		init: function() {
			this.touchable=p.touchable();

			if (this.touchable) {
				document.body.addEventListener('touchstart', this.onTouchStart, true);
				document.body.addEventListener('touchmove', this.onTouchMove, true);
				document.body.addEventListener('touchend', this.onTouchEnd, true);
				document.body.addEventListener('touchcancel', this.onTouchCancel, true)
			};
			document.body.addEventListener('click', this.onClick, true);

		}
	};

	pub.init=function(opts){
		//animation complete callback
		$.fn.animationComplete = function( callback ) {
			var me=this;
			if( p.support.cssTransitions ) {
				return $( this ).one( 'webkitAnimationEnd', function(){
					callback.call(me);
				} );
			}
			else{
				// defer execution for consistency between webkit/non webkit
				setTimeout( function(){
					callback.call(me);
				}, 0 );
				return $( this );
			}
		};
	};

	pub.onLoad=function(){
		p.fastclick.init();
		//test
		var activePage=$("article").addClass("ui-page").eq(0).addClass("ui-page-active");
		
		$(".btn_test,#J_nav_gb a").click(function(e){
			var r="";
			if(this.className.search("back")>-1){
				r=" reverse";
			}
			activePage.addClass("slide out"+r).animationComplete(function(){
				$(this).removeClass("out in reverse slide ui-page-active");
			});
			activePage=$(this.getAttribute("href")).addClass("ui-page-active slide in"+r).animationComplete(function(){
				$(this).removeClass("out in reverse slide");
			});
			e.preventDefault();
		});
	};

	return pub;

})(jQuery));
